﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NugetBuilder
{
    class Program
    {
        public static string NugetKey { get; set; }
        public static string NugetUrl { get; set; }
        static void Main(string[] args)
        {
            Console.Write("请输入要生成nuget 包的项目根目录:");
            string path = Console.ReadLine();
            StringBuilder stringBuilder = new StringBuilder();
            string path2 = Directory.GetCurrentDirectory() + "\\without.txt";
            string path3 = Directory.GetCurrentDirectory() + "\\nuget.txt";
            if (File.Exists(path3))
            {
                var allnugetinfo = File.ReadAllText(path3).Split(',');
                if (allnugetinfo.Length == 2)
                {
                    NugetUrl = allnugetinfo[0];
                    NugetKey = allnugetinfo[1];
                }
                else
                {
                    SaveNuget();
                }
            }
            else
            {
                SaveNuget();
            }
            List<string> list = new List<string>();
            if (File.Exists(path2))
            {
                var without = File.ReadAllText(path2).Split(',');
                if (without.Any())
                {
                    list = without.ToList();
                }
            }
            list.Add(".vs");
            list.Add(".git");
            list = list.Select(x => x.ToLower()).ToList();
            stringBuilder.Append(readFile(new DirectoryInfo(path), 0, list));
            File.WriteAllText(Directory.GetCurrentDirectory() + "\\1.bat", stringBuilder.ToString());
            Console.WriteLine("开始执行1.bat");
            runbat(Directory.GetCurrentDirectory() + "\\1.bat");
            stringBuilder = new StringBuilder();
            stringBuilder.Append(readFile(new DirectoryInfo(path), 1, list));
            File.WriteAllText(Directory.GetCurrentDirectory() + "\\2.bat", stringBuilder.ToString());
            Console.WriteLine("开始执行2.bat");
            runbat(Directory.GetCurrentDirectory() + "\\2.bat");
            if (File.Exists(Directory.GetCurrentDirectory() + "\\1.bat"))
            {
                File.Delete(Directory.GetCurrentDirectory() + "\\1.bat");
            }
            if (File.Exists(Directory.GetCurrentDirectory() + "\\2.bat"))
            {
                File.Delete(Directory.GetCurrentDirectory() + "\\2.bat");
            }
        }
        private static string readFile(DirectoryInfo di, int type, List<string> withoustfilenames)
        {
            StringBuilder sb = new StringBuilder();
            bool flag = !withoustfilenames.Contains(di.Name.ToLower());
            if (flag)
            {
                var files = di.GetFiles().ToList();
                if (type == 0)
                {
                    if (Directory.Exists(di.FullName + "\\bin"))
                    {
                        Directory.Delete(di.FullName + "\\bin", true);
                    }
                    if (Directory.Exists(di.FullName + "\\obj"))
                    {
                        Directory.Delete(di.FullName + "\\obj", true);
                    }
                }
                di.GetDirectories().ToList().ForEach(x=>
                {
                    sb.Append(readFile(x, type, withoustfilenames));
                });
                if (type == 0)
                {
                    if (files.Any(x => x.Extension == ".csproj"))
                    {
                        sb.AppendLine("cd " + di.FullName + " \r\n dotnet pack -c Release");
                    }
                }
                else
                {
                    if (files.Any(x => x.Extension == ".nupkg"))
                    {
                        string fullName = files.FirstOrDefault(x => x.Extension == ".nupkg")?.FullName;
                        sb.AppendLine($"nuget.exe push {fullName} {NugetKey} -Source {NugetUrl} \r\n");
                    }
                }
            }
            return sb.ToString();
        }

        private static void runbat(string batpath)
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            process.StandardInput.WriteLine(batpath + "&exit");
            process.StandardInput.AutoFlush = true;
            process.OutputDataReceived += new DataReceivedEventHandler(Program.OnDataReceived);
            process.BeginOutputReadLine();
            process.WaitForExit();
            bool flag = process.ExitCode != 0;
            if (flag)
            {
                Console.WriteLine(process.StandardError.ReadToEnd());
            }
            process.Close();
        }

        private static void OnDataReceived(object Sender, DataReceivedEventArgs e)
        {
            bool flag = e.Data != null;
            if (flag)
            {
                Console.WriteLine(e.Data);
            }
        }

        private static void SaveNuget()
        {
            Console.Write("请输入要上传nuget包的地址:");
            NugetUrl = Console.ReadLine();
            Console.Write("请输入要上传nuget包的ApiKey:");
            NugetKey = Console.ReadLine();
            string path3 = Directory.GetCurrentDirectory() + "\\nuget.txt";
            File.WriteAllText(path3,$"{NugetUrl},{NugetKey}");
        }
    }
}
